#include "plot.h"


Plot::Plot(QWidget *parent) :
  QwtPlot(parent)
{
  // Assign a title
  //setTitle("10Scope");

  setCanvasBackground(Qt::black);

  alignScales();

  // Insert grid
  d_grid = new QwtPlotGrid();
  d_grid->attach(this);
  d_grid->setPen(QPen(Qt::white));
  d_grid->setVisible(false);

  // Axis
  //  setAxisTitle(QwtPlot::xBottom, "Seconds");
  setAxisScale(QwtPlot::xBottom, -50, 0);

  setAxisTitle(QwtPlot::yLeft, "Values");
  setAxisScale(QwtPlot::yLeft, 0.0, 1000.0);

  setSettings(d_settings);
  setAutoReplot(true);

  xpos=0;
  buffsize=50;
}

void Plot::alignScales()
{
  // The code below shows how to align the scales to
  // the canvas frame, but is also a good example demonstrating
  // why the spreaded API needs polishing.

  canvas()->setFrameStyle(QFrame::Box | QFrame::Plain );
  canvas()->setLineWidth(1);

  for ( int i = 0; i < QwtPlot::axisCnt; i++ )
  {
    QwtScaleWidget *scaleWidget = (QwtScaleWidget *)axisWidget(i);
    if ( scaleWidget )
      scaleWidget->setMargin(0);

    QwtScaleDraw *scaleDraw = (QwtScaleDraw *)axisScaleDraw(i);
    if ( scaleDraw )
      scaleDraw->enableComponent(QwtAbstractScaleDraw::Backbone, false);
  }

  plotLayout()->setAlignCanvasToScales(true);
}

void Plot::newcanvasColor(QColor newColor)
{
  setCanvasBackground(QBrush(newColor));
  replot();
}

void Plot::toggleGrid(bool gridOn)
{
  d_grid->setVisible(gridOn);
}

void Plot::newgridColor(QColor newColor)
{
  d_grid->setPen(QColor(newColor));
}

void Plot::newcurveColor(QColor newColor)
{
  //QPen mypen=d_curve->pen();
 // mypen.setColor(newColor);
 // d_curve->setPen(mypen);
}

void Plot::newcurveWidth(int newWidth)
{
  //QPen mypen=d_curve->pen();
 // mypen.setWidth(newWidth);
 // d_curve->setPen(mypen);
}

void Plot::newbuffSize(int newSize)
{
  buffsize = newSize;
  setAxisScale(QwtPlot::xBottom, (newSize*(-1)), 0);
}

void Plot::setSettings(const Settings &s)
{
  QPen mypen;
  mypen.setWidth(3);
  mypen.setColor(Qt::white);
 // d_curve->setPen(mypen);
 // d_curve->setBrush(s.curve.brush);

//  d_curve->setRenderHint(QwtPlotCurve::RenderAntialiased);
}

void Plot::newVariable()
{
  QVector<QPointF> newVector;
  vectorList.append(newVector);
  QwtPlotCurve *newCurve = new QwtPlotCurve;
  QPen mypen;
  mypen.setWidth(3);
  mypen.setColor(Qt::white);
  newCurve->setPen(mypen);
  newCurve->setRenderHint(QwtPlotCurve::RenderAntialiased);
  newCurve->attach(this);
  curveList.append(newCurve);
}

void Plot::removeVariable(int index)
{
  vectorList.removeAt(index);
  delete curveList.at(index);
  //curveList.takeAt(index);
  curveList.removeAt(index);
}

void Plot::newSample(QList<variable> varList)
{
  int i=0,j=0;
  xpos--;
  if(xpos<=(buffsize*(-1)))
    xpos=0;

  for(j=0;j<vectorList.size();j++)
  {
    for(i=0;i<vectorList.at(j).size();i++)
    {
      vectorList[j][i].setX((qreal)vectorList.at(j).at(i).x()-1);
//      numList[i].setX((qreal)numList.at(i).x()-1);
    }
  }

  if(varList.size()>0)
  {
    for(i=0;i<varList.size();i++)
    {
      if(varList.at(i).type==NUMBER)
      {
        vectorList[i].append(QPointF(0,varList.at(i).floatvalue));
        //numList.append(QPointF(0,varList.at(i).floatvalue));
        while(vectorList[i].size()>buffsize)
        {
          vectorList[i].remove(0);
//          numList.remove(0);
        }
        curveList[i]->setSamples(vectorList[i]);
        curveList[i]->setVisible(showCurve.at(i));
      }
    }
    //d_curve->setSamples(numList);
   // i=0; while(varList.at(i).type!=NUMBER && i<varList.size()) i++;
   // if(varList.at(i).type==NUMBER) d_curve->setSamples(vectorList[i]);
    //d_curve->setVisible(true);
  }
}

void Plot::plotSelection(QList<bool> newSelection)
{
showCurve=newSelection;
}

void Plot::takeSnapshot()
{
  QPixmap qPix = QPixmap::grabWidget(this);
  if(qPix.isNull()){
    qDebug("Failed to capture the plot for saving");
    return;
  }
  QString types(	"JPEG file (*.jpg);;"				// Set up the possible graphics formats
                  "Portable Network Graphics file (*.png);;"
                  "Bitmap file (*.bmp)");
  QString filter;							// Type of filter
  QString jpegExt=".jpg", pngExt=".png", bmpExt=".bmp";		// Suffix for the files
  QString suggestedName=QString("snapshot");
  QString fn = QFileDialog::getSaveFileName(this,tr("Save Image"),
                                            suggestedName,types,&filter);

  if ( !fn.isEmpty() ) {						// If filename is not a null
    if (fn.contains(jpegExt)) {				// Remove file extension is already there
      fn.remove(jpegExt);
    }
    else if (fn.contains(pngExt)) {
      fn.remove(pngExt);
    }
    else if (fn.contains(bmpExt)) {
      fn.remove(bmpExt);
    }
    if (filter.contains(jpegExt)) {				// OR, Test to see if jpeg and save
      fn+=jpegExt;
      qPix.save( fn, "JPEG" );
    }
    else if (filter.contains(pngExt)) {			// OR, Test to see if png and save
      fn+=pngExt;
      qPix.save( fn, "PNG" );
    }
    else if (filter.contains(bmpExt)) {			// OR, Test to see if bmp and save
      fn+=bmpExt;
      qPix.save( fn, "BMP" );
    }
  }
}
